package com.onetime.mall.trading.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.onetime.mall.trading.dao.UserDao;
import com.onetime.mall.trading.entity.UserEntity;
import com.onetime.mall.trading.service.UserService;
import com.onetime.mall.trading.utils.PageUtils;
import com.onetime.mall.trading.utils.Query;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = params.get("key").toString();
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        if (key != null && !StringUtils.isEmpty(key)) {
            wrapper.eq("id", key)
                    .or().like("account_number",key)
                    .or().like("nickname",key);
        }

        IPage<UserEntity> page = this.page(
                new Query<UserEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    /**
     * 用户登录
     *
     * @param accountNumber 账号
     * @param userPassword  密码
     * @return 用户类
     */
    @Override
    public UserEntity userLogin(String accountNumber, String userPassword) {
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("account_number", accountNumber);
        UserEntity user = baseMapper.selectOne(wrapper);

        if (user != null) {
            /*密码匹配*/
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            boolean matches = encoder.matches(userPassword, user.getUserPassword());
            if (matches) {
                return baseMapper.selectOne(wrapper);
            } else {
                return null;
            }
        } else {
            return null;
        }
        /* .eq("user_password", userPassword)*/

    }

    /**
     * 注册用户信息
     *
     * @param user 用户
     * @return true/false
     */
    @Override
    public boolean userSignIn(UserEntity user) {
        String accountNumber = user.getAccountNumber();
        String nickname = user.getNickname();
        String userPassword = user.getUserPassword();

        /*手机号查重*/
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("account_number", accountNumber);
        UserEntity accountNumberOne = baseMapper.selectOne(wrapper);

        /*昵称查重*/
        QueryWrapper<UserEntity> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("nickname", nickname);
        UserEntity nicknameOne = baseMapper.selectOne(wrapper1);

        if (accountNumberOne == null && nicknameOne == null) {
            /*MD5盐值加密*/
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            String encode = encoder.encode(userPassword);
            user.setUserPassword(encode);
            int insert = baseMapper.insert(user);
            return insert == 1;
        } else {
            return false;
        }
    }


    /**
     * 修改密码
     *
     * @param newPassword 新密码
     * @param oldPassword 旧密码
     * @param id          用户ID
     * @return true or false
     */
    @Override
    public boolean updatePassword(String newPassword, String oldPassword, Long id) {
        /*查找当前用户原始信息*/
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        UserEntity user = baseMapper.selectOne(wrapper);
        /*密码匹配*/
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        boolean matches = encoder.matches(oldPassword, user.getUserPassword());

        if (StringUtils.isNotEmpty(newPassword) && matches) {
            /*设置新密码*/
            String encode = encoder.encode(newPassword);
            user.setUserPassword(encode);
            int update = baseMapper.updateById(user);
            return update == 1;
        } else {
            return false;
        }
    }


    /**
     * 修改用户公开信息，未验证用户身份
     *
     * @return
     */
    @Override
    public boolean updateUserInfo(UserEntity userEntity) {
        int update = baseMapper.updateById(userEntity);
        return update == 1;
    }

    @Override
    public PageUtils getUserByStatusByPage(Map<String, Object> params) {
        String status = (String) params.get("status");
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();

        if (status.equals("0")) {
            wrapper.ne("user_status", 1);
            List<UserEntity> userEntities = baseMapper.selectList(wrapper);
            IPage<UserEntity> page = this.page(new Query<UserEntity>().getPage(params), wrapper);

            PageUtils pageUtils = new PageUtils(page);
            pageUtils.setList(userEntities);

            return pageUtils;
        } else {
            wrapper.eq("user_status", 1);
            List<UserEntity> userEntities = baseMapper.selectList(wrapper);
            IPage<UserEntity> page = this.page(new Query<UserEntity>().getPage(params), wrapper);

            PageUtils pageUtils = new PageUtils(page);
            pageUtils.setList(userEntities);

            return pageUtils;
        }

    }

}